package com.taxshare.operation.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taxshare.operation.entity.TenantEntity;
import com.taxshare.operation.mapper.TenantMapper;
import com.taxshare.operation.view.dto.ResourceDTO;
import com.taxshare.operation.view.dto.RoleResourceDTO;
import com.taxshare.response.beans.Response;
import com.taxshare.tools.token.UserInfoUtil;
import com.taxshare.tools.util.UUIDUitl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @ClassName: TenantService
 * @Description:
 * @author: luozhongyi
 * @date: 2021/2/20 17:41
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
public class TenantService extends ServiceImpl<TenantMapper, TenantEntity> {


    /**
     * 初始化系统信息【适用于定制项目】
     */
    public Response initSystem() {
        //获取超级管理员id
        String roleId = baseMapper.queryRoleId();
        //获取全部菜单
        List<ResourceDTO> resourceDTOS = baseMapper.queryResources();
        //给超级管理员角色重新赋值全部菜单
        baseMapper.deleteRoleResourceByRoleId(roleId);
        List<RoleResourceDTO> roleResourceDTOS = new ArrayList<>();
        RoleResourceDTO roleResourceDTO;
        for (ResourceDTO dto :resourceDTOS){
            roleResourceDTO = new RoleResourceDTO();
            roleResourceDTO.setUuid(UUIDUitl.UUID32(false));
            roleResourceDTO.setRoleId(roleId);
            roleResourceDTO.setResourceId(dto.getResourceId());
            roleResourceDTO.setCreateTime(new Date());
            roleResourceDTO.setCreateUserid(UserInfoUtil.getUserID());
            roleResourceDTOS.add(roleResourceDTO);
        }
        baseMapper.insertRoleResourceBatch(roleResourceDTOS);
        return Response.success();
    }
}
